package mbean;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

import util.FacesUtil;
import vo.OrganizationVO;
import vo.StrategicPlanVO;
import vo.UsuarioVO;
import dao.StrategyDAO;
import dao.UsuarioDAO;

public class StrategyMBean extends AbstractMBean{
	
	private Date dataCriacao, dataFim;
	private int strategicPlanId, backSwot, selectedOrganization, selectedStrategicPlan;
	private String nome, descricao, nomeOrganizacao;
	private List<SelectItem> listEmpresa;	
	private List<StrategicPlanVO> listStrategicPlan;
	private boolean menuSaveDisabled;
	
	public StrategyMBean(){
		this.backSwot = 0;
		this.menuSaveDisabled = true;
	}
	
	public Date getDataCriacao() {
		return dataCriacao;
	}

	public void setDataCriacao(Date dataCriacao) {
		this.dataCriacao = dataCriacao;
	}

	public Date getDataFim() {
		return dataFim;
	}

	public void setDataFim(Date dataFim) {
		this.dataFim = dataFim;
	}

	public String save(){
		if (validaTela()){
			StrategyDAO strategyDAO = new StrategyDAO();
			StrategicPlanVO vo = new StrategicPlanVO();
			vo.setDataCriacao(dataCriacao);
			vo.setDataFim(dataFim);			
			vo.setNome(nome);
			vo.setDescricao(descricao);
			vo.setOrganizacaoId(selectedOrganization);
			
			if (this.strategicPlanId > 0){
				vo.setStrategicPlanId(strategicPlanId);
				strategyDAO.updateStrategicPlan(vo);
				setaTela(false, "strategy");
				return "back";
			}
			else{
				int usuarioId = ((UsuarioVO)FacesUtil.getSessionMapValue("usuario")).getId();
				vo.setUsuarioId(usuarioId);
				this.strategicPlanId = strategyDAO.insertStrategy(vo);
				setaTela(true, "strategy");
				return "insert";
			}
		}
		else{
			return "";
		}
	}

	private boolean validaTela() {
		boolean retorno = true;
		
		if (this.dataCriacao == null){
			retorno = false;
			FacesUtil.addErrorMessage(FacesUtil.getMessageResourceString("errorInitialDateRequired"));
		}
		
		if (this.dataFim == null){
			retorno = false;
			FacesUtil.addErrorMessage(FacesUtil.getMessageResourceString("errorEndDateRequired"));
		}
		
		if (this.nome.equalsIgnoreCase("")){
			retorno = false;
			FacesUtil.addErrorMessage(FacesUtil.getMessageResourceString("errorNameRequired"));
		}
		
		if (this.selectedOrganization <= 0){
			retorno = false;
			FacesUtil.addErrorMessage(FacesUtil.getMessageResourceString("errorOrganizationRequired"));
		}
		
		return retorno;
	}

	public void setDescricao(String descricao) {
		this.descricao = descricao;
	}

	public String getDescricao() {
		return descricao;
	}

	public void setNome(String nome) {
		this.nome = nome;
	}

	public String getNome() {
		return nome;
	}

	public void setListEmpresa(List<SelectItem> listEmpresa) {
		this.listEmpresa = listEmpresa;
	}

	public List<SelectItem> getListEmpresa() {
		populaComboEmpresa();
		return listEmpresa;
	}

	public String abreTelaInsercao(){
		populaComboEmpresa();
		this.menuSaveDisabled = false;
		this.strategicPlanId = 0;
		setaTela(true, "strategy");
		
		return "new-strategic-plan";
	}

	private void populaComboEmpresa() {		
		SelectItem si;
		listEmpresa = new ArrayList<SelectItem>();
		
		int idLogin = ((LoginMBean) FacesUtil.getSessionMapValue("login")).getId();
		if (idLogin > 0){
			FacesContext facesContext = FacesContext.getCurrentInstance();
			String currentPage = facesContext.getViewRoot().getViewId();
			
			if (currentPage.lastIndexOf("login.xhtml") == -1){
				si = new SelectItem();
				si.setLabel(FacesUtil.getMessageResourceString("allLabel"));
				si.setValue("");
				listEmpresa.add(si);
				
				UsuarioDAO uDAO = new UsuarioDAO();
				Map<String, OrganizationVO> mapa = uDAO.getOrganizations(idLogin);
				
				for(String key: mapa.keySet()){
					si = new SelectItem();
					si.setLabel(mapa.get(key).getNome());
					si.setValue(mapa.get(key).getId());
					listEmpresa.add(si);
				}
			}
		}
	}

	public void setListStrategicPlan(List<StrategicPlanVO> listStrategicPlan) {
		this.listStrategicPlan = listStrategicPlan;
	}

	public List<StrategicPlanVO> getListStrategicPlan() {
		return listStrategicPlan;
	}
	
	public String listStrategicPlan(){
		StrategyDAO dao = new StrategyDAO();
		
		if (((LoginMBean) FacesUtil.getSessionMapValue("login")).getId() > 0){
			listStrategicPlan = dao.listStrategy(selectedOrganization, ((LoginMBean) FacesUtil.getSessionMapValue("login")).getId());
			populaComboEmpresa();
			setaTela(false, "strategy");
			
			return "search-strategic-plan";
		}
		else
			return "";
	}
	
	public void setNomeOrganizacao(String nomeOrganizacao) {
		this.nomeOrganizacao = nomeOrganizacao;
	}

	public String getNomeOrganizacao() {
		return nomeOrganizacao;
	}
	
	public void setStrategicPlanId(int strategicPlanId) {
		this.strategicPlanId = strategicPlanId;
	}

	public int getStrategicPlanId() {
		return strategicPlanId;
	}

	public void setBackSwot(int backSwot) {
		this.backSwot = backSwot;
	}

	public int getBackSwot() {
		return backSwot;
	}

	public void setSelectedOrganization(int selectedOrganization) {
		this.selectedOrganization = selectedOrganization;
	}

	public int getSelectedOrganization() {
		return selectedOrganization;
	}
	
	public boolean getStrategyChosen(){
		return (this.nomeOrganizacao != null) && (! (this.nomeOrganizacao.equalsIgnoreCase("")));
	}
	
	public String changeOrganization(){
		return "changeOrganization";
	}

	public void setSelectedStrategicPlan(int selectedStrategicPlan) {
		this.selectedStrategicPlan = selectedStrategicPlan;
	}

	public int getSelectedStrategicPlan() {
		return selectedStrategicPlan;
	}

	public void setMenuSaveDisabled(boolean menuSaveDisabled) {
		this.menuSaveDisabled = menuSaveDisabled;
	}

	public boolean getMenuSaveDisabled() {
		return menuSaveDisabled;
	}

	@Override
	public boolean isDirty() {
		boolean dirty = false;
		
		if (this.strategicPlanId == 0){
			dirty = true;
		}
		else{
			StrategyDAO dao = new StrategyDAO();
			StrategicPlanVO vo = dao.getStrategicPlan(this.strategicPlanId);
			
			if (vo.getDataCriacao().compareTo(this.dataCriacao) != 0)			
				dirty = true;

			if (vo.getDataFim().compareTo(this.dataFim) != 0)
				dirty = true;
			
			if (! vo.getNome().equalsIgnoreCase(this.nome))
				dirty = true;
			
			if (! vo.getDescricao().equalsIgnoreCase(this.descricao))
				dirty = true;
			
			if (vo.getOrganizacaoId() != this.selectedOrganization)
				dirty = true;
		}
		
		return dirty;
	}
	
	public String openView(){
		setaTela(true, "strategy");
		return "view";
	}
}
